//
//  asdfasdfViewController.m
//  EcologyME
//
//  Created by 李雪虎 on 2017/8/15.
//  Copyright © 2017年 xzcysoft. All rights reserved.
//

#import "asdfasdfViewController.h"
#import "ZZYPhotoHelper.h"
@interface asdfasdfViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    UIImage*img1;
    UIImage*img2;
    UIImage*img3;
    NSString*imgType;
    NSInteger row1;
    NSInteger row2;
    NSInteger row3;
    UIButton *dizhibtn;
}
@property(nonatomic,strong)UIView *chengshiBigView;//城市背景
@property(nonatomic, strong) UIPickerView * pickerView;//
@property(nonatomic, strong) UIView * toolBar;
@property(nonatomic,strong)NSArray *oenARR;

@property(nonatomic, strong) UIButton * cancleBtn;
/**
 选中的市
 */
@property(nonatomic, copy) NSString * cityStr;
@property(nonatomic, strong) UIButton * sureBtn;
@end

@implementation asdfasdfViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden=NO;
     self.title = @"商户认证";
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)qiyexingzhiBtnClciked:(id)sender {
}
- (IBAction)xuanZeChengShi:(id)sender {
    NSLog(@"选择城市");
    dizhibtn = (UIButton *)sender;
    [self addChengshiBigView];
}
- (IBAction)yingYeZhiZhaoBtnClicke:(id)sender {
    NSLog(@"营业执照");
//    UIButton *button = (UIButton *)sender;
    imgType=@"13";
    [[ZZYPhotoHelper shareHelper] showImageViewSelcteWithResultBlock:^(id data) {
        [sender setBackgroundImage:data forState:0];
//        [_yingYeZhiZhaoBtn setImage:data forState:0];
        img1=data;
//        [self tijiaoTuPianHttp:img1];
        
    }];
    
}
- (IBAction)dianMianZhaoPianBtnClicke:(id)sender {
    imgType=@"13";
    [[ZZYPhotoHelper shareHelper] showImageViewSelcteWithResultBlock:^(id data) {
        [sender setBackgroundImage:data forState:0];
        img1=data;
//        [self tijiaoTuPianHttp:img2];
        
    }];
    NSLog(@"店面照片");
}
- (IBAction)weiShengXuKeZhengBtnClike:(id)sender {
    NSLog(@"赢也许可证");
    imgType=@"13";
    [[ZZYPhotoHelper shareHelper] showImageViewSelcteWithResultBlock:^(id data) {
        [sender setBackgroundImage:data forState:0];
        img1=data;
//        [self tijiaoTuPianHttp:img3];
        
    }];
}
- (IBAction)baoCunBtnClicke:(id)sender {
    NSLog(@"保存");
}
-(void)tijiaoTuPianHttp:(UIImage*)imamg
{      NSString*tokenStr= OBJECT_FOR_KEY(@"token");
    NSString*urlStr=[NSString stringWithFormat:@"common/file/upload.api?access_token=%@",tokenStr];
    
    NSData*data;
    imamg=  [self scaleToSize:imamg size:CGSizeMake(150, 150)];
    if (UIImagePNGRepresentation(imamg)) {
        data=  UIImagePNGRepresentation(imamg);
        
    }else
    {
        data=  UIImageJPEGRepresentation(imamg,1);
        
    }
    NSLog(@"%@",data);
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         
                                                         @"text/html",
                                                         
                                                         @"image/jpeg",
                                                         
                                                         @"image/png",
                                                         
                                                         @"application/octet-stream",
                                                         
                                                         @"text/json",
                                                         
                                                         nil];
    
    manager.requestSerializer= [AFHTTPRequestSerializer serializer];
    
    manager.responseSerializer= [AFHTTPResponseSerializer serializer];
    
    [manager POST:LINK_BASE_URL(urlStr)  parameters:@{@"userId":userid,@"category":imgType} constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        //        UIImage *image = [UIImage imageNamed:@"gauge.png"];
        //
        //        NSData *data = UIImagePNGRepresentation(image);
        
        //上传的参数(上传图片，以文件流的格式)
        
        [formData appendPartWithFileData:data
         
                                    name:@"uploadFile"
         
                                fileName:@"gauge.jpg"
         
                                mimeType:@"image/jpg"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"上传成功");
        NSLog(@"%@",responseObject);
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",dict);
//        if ([imgType isEqualToString:@"13"]) {
//            zhengmianStr=[NSString stringWithFormat:@"%@",dict[@"data"]];
//        }else{
//            fanmianStr=[NSString stringWithFormat:@"%@",dict[@"data"]];
//        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"上传失败%@",error);
        
        
        
    }];
    
}
-(UIImage *)scaleToSize:(UIImage *)image size:(CGSize)size
{
    //创建一个bitmap的context
    //并把他设置成当前的context
    UIGraphicsBeginImageContext(size);
    //绘制图片的大小
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    //从当前context中创建一个改变大小后的图片
    UIImage *endImage=UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    return endImage;
}




#pragma mark -- 从plist里面读数据
- (void)loadDatas
{
    
    NSData *fileData = [[NSData alloc]init];
    NSString *path;
    path = [[NSBundle mainBundle] pathForResource:@"shengshiqu" ofType:@"json"];
    fileData = [NSData dataWithContentsOfFile:path];
    
    self.oenARR= [NSArray array];
    
    self.oenARR = [NSJSONSerialization JSONObjectWithData:fileData options:NSJSONReadingMutableLeaves error:nil];
    
    
    
}
#pragma mark--城市View
-(void)addChengshiBigView{
    [self loadDatas];
    row1=0;
    row2=0;
    row3=0;
    _chengshiBigView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width_screen, height_screen)];
    _chengshiBigView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_chengshiBigView];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, height_screen-356, width_screen, 356)];
    view.backgroundColor = [UIColor whiteColor];
    [_chengshiBigView addSubview:view];
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 356 - 316, width_screen, 316)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    [view addSubview:_pickerView];
    _toolBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width_screen, 356 - 316)];
    _toolBar.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [view addSubview:_toolBar];
    
    _cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancleBtn.frame = CGRectMake(10, 5, 50, 356 - 316 - 10);
    [_cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    _cancleBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _cancleBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    _cancleBtn.layer.borderWidth = 0.5;
    _cancleBtn.layer.cornerRadius = 5;
    _cancleBtn.backgroundColor = [UIColor orangeColor];
    [_cancleBtn addTarget:self action:@selector(cancleBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_toolBar addSubview:_cancleBtn];
    _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _sureBtn.frame = CGRectMake(width_screen - 60, 5, 50, 356 - 316 - 10);
    [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    _sureBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _sureBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    _sureBtn.layer.borderWidth = 0.5;
    _sureBtn.layer.cornerRadius = 5;
    _sureBtn.backgroundColor = [UIColor orangeColor];
    [_sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_toolBar addSubview:_sureBtn];
    
    
    
}


#pragma mark -- UIPickerView
//返回显示的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}
//返回当前列显示的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.oenARR.count;
    }else if (component == 1){
        NSArray *array =self.oenARR[row1][@"city"];
        if ((NSNull*)array != [NSNull null]) {
            return array.count;
        }
    }else if (component ==2){
        NSArray *array =self.oenARR[row1][@"city"][row2][@"area"];
        if ((NSNull*)array != [NSNull null]) {
            return array.count;
        }
    }
    return 0;
}
//设置当前行的内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if(component == 0) {
        
        return self.oenARR[row][@"value"];
    }
    else if (component == 1) {
        return self.oenARR[row1][@"city"][row2][@"value"];
    }else if (component==2){
        return self.oenARR[row1][@"city"][row2][@"area"][row3][@"value"];
    }
    return nil;
}
//每行显示的文字样式
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width_screen / 3, 30)];
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = NSTextAlignmentCenter;
    if (component == 0) {
        label.text = self.oenARR[row][@"value"];
    }else if (component == 1){
        label.text = self.oenARR[row1][@"city"][row][@"value"];
    }else if (component==2){
        label.text = self.oenARR[row1][@"city"][row2][@"area"][row][@"value"];
    }
    return label;
}
//选择的行数
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {//选择省
        row1 = row;
        row2 = 0;
        row3=0;
        [self.pickerView reloadComponent:1];
        [self.pickerView reloadComponent:2];
        [self.pickerView selectRow:0 inComponent:1 animated:YES];
        [self.pickerView selectRow:0 inComponent:2 animated:YES];
        
    }else if (component == 1){//选择市
        row2 = row;
        row3=0;
        [self.pickerView reloadComponent:2];
        [self.pickerView selectRow:0 inComponent:2 animated:YES];
    }else if (component==2){
        row3=row;
    }
    NSInteger cityRow1 = [self.pickerView selectedRowInComponent:0];
    NSInteger cityRow2 = [self.pickerView selectedRowInComponent:1];
    NSInteger cityRow3 = [self.pickerView selectedRowInComponent:2];
    NSString *str1 = self.oenARR[cityRow1][@"value"];
    NSString *str2 = self.oenARR[cityRow1][@"city"][cityRow2][@"value"];
    NSString *str3 = self.oenARR[cityRow1][@"city"][cityRow2][@"area"][cityRow3][@"value"];
    
    NSString *bianma = self.oenARR[cityRow1][@"city"][cityRow2][@"area"][cityRow3][@"key"];
    NSLog(@"省===%@",str1);
    NSLog(@"市===%@",str2);
    NSLog(@"区===%@",str3);
    NSLog(@"编码===%@",bianma);
    NSLog(@"%@",[NSString stringWithFormat:@"%@-%@-%@",str1,str2,str3]);
    self.cityStr =[NSString stringWithFormat:@"%@-%@-%@",str1,str2,str3];
}


- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 40.0;
}

#pragma mark -- Button
- (void)cancleBtnClick
{
    [_chengshiBigView removeFromSuperview];
    _chengshiBigView.alpha=0.0;
}

- (void)sureBtnClick
{
//    [USER_D setObject:_provinceStr forKey:@"cityStr"];
    
//    [locationbutton setTitle:self.cityStr forState:UIControlStateNormal];
//    [self httpRquestRenWuUrl];
    [dizhibtn setTitle:self.cityStr forState:UIControlStateNormal];
//    [_yingYeDiZhiBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_chengshiBigView removeFromSuperview];
    
    _chengshiBigView.alpha=0.0;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
